#include "Scene.h"
#include "Camera.h"
#include "Engine.h"

extern Camera camera;
bool pauseState;

Scene::Scene()
{    	
}

// Remove all object out of scene.
Scene::~Scene()
{
    for (modelIterator iterator = models.begin(); iterator != models.end(); iterator++)
    {
		Model3D *model3D = *iterator;
        SAFE_DEL(model3D);
    }
}

// Add an Object
void Scene::AddObject(Model3D *model)
{
    models.push_back(model);
}

// Render all object.
// 1. Caculate the transformation matrix
// Transformation = ProjectionMatrix * ViewMatrix * WorldMatrix
// 2. Set the Uniform uWvpMatrix
// 3. Render The Object
void Scene::Render()
{   
	Matrix viewProjection;
    
	glViewport(0, 0, SCREEN_W, SCREEN_H);
	// ProjectionMatrix goes here.
	viewProjection.LoadPerperstive(45, SCREEN_W / (float) SCREEN_H, 0.1f, 200.0f);
    
	// Mutiply with View Matrix  (Camera Matrix)
	viewProjection*= *camera.GetTransform();
   
	for (modelIterator iterator = models.begin(); iterator != models.end(); iterator++)
	{
		Model3D *model3D = *iterator;	
		if(model3D->alive)
		{			
			Matrix wvpMatrix = viewProjection;
			// Multiply with World Matrix
			wvpMatrix*= *model3D->GetTransform();
			glUseProgram(Uniforms::program);
			glUniformMatrix4fv(Uniforms::uWvpMatrix, 1, GL_FALSE, wvpMatrix.value);        
			// Render the Object
			model3D->Render();				
		}
		
	}    
}